package com.plian.system.service.pf.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.DateUtil;
import com.plian.system.entity.pf.AppraisalIndex;
import com.plian.system.entity.pf.LeadershipPayRecordDeclare;
import com.plian.system.mapper.pf.LeadershipPayRecordDeclareMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.pf.IAppraisalIndexService;
import com.plian.system.service.pf.ILeadershipPayRecordDeclareService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2023/3/27 2:17 PM
 * @Param
 * @return
 * @Version: 1.0
 **/
@Slf4j
@Service
@AllArgsConstructor
public class LeadershipPayRecordDeclareServiceImpl extends BaseServiceImpl<LeadershipPayRecordDeclareMapper, LeadershipPayRecordDeclare> implements ILeadershipPayRecordDeclareService {

    private final IAppraisalIndexService appraisalIndexService;

    @Override
    public boolean deleteByParentId(List<Long> ids) {
        LambdaQueryWrapper<LeadershipPayRecordDeclare> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(LeadershipPayRecordDeclare::getBaseId, ids);
        List<LeadershipPayRecordDeclare> payRecordDeclares = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(payRecordDeclares)){
            return deleteLogic(payRecordDeclares.stream().map(LeadershipPayRecordDeclare::getId).collect(Collectors.toList()));
        }
        return true;
    }

    @Override
    public List<AppraisalIndex> getIndex(){
        List<AppraisalIndex> indexList = appraisalIndexService.listByTableCodeAndYear("006", DateUtil.format(new Date(), DateUtil.PATTERN_YEAR));
        return indexList.stream().filter(m -> Optional.ofNullable(m.getSort()).isPresent()).sorted(Comparator.comparing(AppraisalIndex::getSort)).collect(Collectors.toList());
    }
}
